#!/bin/bash

set -ex

if [ ! -f containerd-1.7.2-linux-amd64.tar.gz ]; then
wget http://10.250.11.185/downloads/k8s/containerd-1.7.2-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-1.7.2-linux-amd64.tar.gz
fi

if [ ! -f /etc/containerd/config.toml ]; then
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
fi


if [ ! -f /lib/systemd/system/containerd.service ]; then
wget http://10.250.11.185/downloads/k8s/containerd.service -O /lib/systemd/system/containerd.service
systemctl daemon-reload
systemctl enable --now containerd
fi

if [ ! -f runc.amd64 ]; then
wget http://10.250.11.185/downloads/k8s/runc.amd64
install -m 755 runc.amd64 /usr/local/sbin/runc
fi

if [ ! -f cni-plugins-linux-amd64-v1.3.0.tgz ]; then
wget http://10.250.11.185/downloads/k8s/cni-plugins-linux-amd64-v1.3.0.tgz
mkdir -p /opt/cni/bin
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.3.0.tgz
fi

if [ ! -f /etc/modules-load.d/k8s.conf ]; then
cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
fi

modprobe overlay
modprobe br_netfilter

# sysctl params required by setup, params persist across reboots
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# Apply sysctl params without reboot
sysctl --system
lsmod | grep br_netfilter
lsmod | grep overlay

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1


sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward

grep "net.ipv6.conf.all.disable_ipv6=1" /etc/sysctl.conf
if [ $? -eq 1 ]; then
cat << EOF >> /etc/sysctl.conf

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1

EOF

fi

swapoff -a
sed -i '/swap/d' /etc/fstab
